<?php
/* 
 *通用产品管理
 */
class Common_productAction extends CommonAction {
    public function _filter(&$map) {
        $key = trim($_REQUEST['keywords']);
        if(strpos($key, ' ')){ //高级搜索，仅搜索产品名称

            $map = A('Search')->search($key,'Common_product');
        }else{
            if(!empty($key)) {
                $map['name'] = array('like',"%".$key."%");//备注信息
                //$map['title'] = array('like',"%".$key."%");//锚文本
                //$map['_logic'] = "or";
            }
        }
    }


    public function _before_edit(){
        $this->assign('cate_list',D('Category')->getAllCatalog(0,0));
        $this->assign('plist',M('Product')->where('common_id = '.$_REQUEST['id'])->findAll() );
    }
    public function _before_add(){
       $this->_before_edit( );    
    
    }

    public function update(){
        if($_POST['is_create_goods'] == "on" ){
            $product = M('Common_product') ->getById($_POST['id']);
            $data = array( 
                'common_id' => $_POST['id'],
                'name' => $_POST['name'],
                'brand_id' => $_POST['brand_id'],
                'cat_id' => $_POST['cat_id'],
                'unit_id' => $_POST['unit_id'],
                'cost_default' => $_POST['cost_default'],
                'sale_price_default' => $_POST['sale_price_default'],
                'wholesale_price_default' => $_POST['wholesale_price_default'],
                'pnumber' => $product['mano'].getModelName($_POST['brand_id'], 'Brand','mano' ),
                'supplier_id_default' => $_POST['supplier_id_default'],
                'warehouse_id' => $_POST['warehouse_id'],
                'create_time' => time(),
                );
            M('Product')->add($data);
        }
        parent::update();
    }

    //批量添加唯一货品
    public function mass_add_unique_goods(){
        $id = $_REQUEST['cid'];
        if (isset ( $id )) {
            $condition = array ('id' => array ('in', explode ( ',', $id ) ) );
            $list=M('Common_product')->where ( $condition )->findAll();
            if(!is_array($list))
                $this->error('您选择的产品不存在！');

            $sdata = array();
            $product =  M('Product');
            $i = 0;
            foreach($list as $val) {
                $rs =   $product->where('common_id ='.$val['id'])->findAll(); 
                //已经有货品的 ，则不再添加
                if(is_array($rs))
                   continue;
                $sdata = array(
                    'common_id' => $val['id'],
                    'name' => $val['name'],
                    'brand_id' => $val['brand_id'],
                    'cat_id' => $val['cat_id'],
                    'unit_id' => $val['unit_id'],
                    'pnumber' => $val['mano'].getModelName($val['brand_id'], 'Brand','mano' ),
                    'cost_default' => $val['cost_default'],
                    'sale_price_default' => $val['sale_price_default'],
                    'wholesale_price_default' => $val['wholesale_price_default'],
                    'supplier_id_default' => $val['supplier_id_default'],
                    'warehouse_id' => $val['warehouse_id'],
                    'create_time' => time(),
                    );
                 $product->add($sdata);
                 $i ++;
            }
            $this->success('成功添加'.$i.' 个货品！',false,5);
        }else{
            $this->error ( '非法操作' );
        }
    }

}
?>
